﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;

using System.Text;
using System.Windows.Forms;
using QUANLYCHAMCONG.BL.Entities;
using QUANLYCHAMCONG.BL.BUS;
using QUANLYCHAMCONG.BL.BUS.Import;
using Microsoft.VisualBasic.CompilerServices;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Runtime.InteropServices;
using Hopthoai;

namespace QUANLYCHAMCONG.PL.CMS.BaoCao
{
    public partial class baocaotheogionguon : UserControl
    {
      
        System.Data.DataTable dt = new System.Data.DataTable();
        public static Microsoft.Office.Interop.Excel.Application ExApp;
        public static Workbook TasWBook;
        public static Worksheet TasWSheet = new Worksheet();
        public baocaotheogionguon()
        {
            InitializeComponent();
        }
        public void loadPhongban()
        {
            System.Data.DataTable phongban = new System.Data.DataTable();
            phongban = PHONGBANBUS.SelectAll();
            if (phongban.Rows.Count != 0)
            {
                Common.AddDataForCombobox(ref this.cmbphongban, ref phongban, "TENPB", "MAPB");
            }
        }
        private void SetLink()
        {
            this.links1.SetValues("Báo cáo", "Giờ nguồn");
        }
        private void baocaotheogionguon_Load(object sender, EventArgs e)
        {
            loadPhongban();
            SetLink();
        }
        private void btntimkiem_Click(object sender, EventArgs e)
        {
            //thuc hien tim kiem theo gio nguon
            string MAPB;
            if (cmbphongban.SelectedIndex != -1 && cmbphongban.SelectedValue.ToString() != "")
            {
                if (DateTime.Compare(dtpngaybd.Value, dtpngaykt.Value) < 0)
                {
                    MAPB = cmbphongban.SelectedValue.ToString();
                    if (!String.IsNullOrEmpty(MAPB))
                    {
                        BAOCAO bc = new BAOCAO();
                        bc.MAPB = MAPB;
                        bc.MANV = null;
                        bc.NGAYBD = dtpngaybd.Value.Date;
                        bc.NGAYKT = dtpngaykt.Value.Date;
                        dt = BAOCAOBUS.GIONGUON(bc);
                        this.dgvgionguon.DataSource = dt;
                    }
                }
                else
                {
                    frmHoidap hoidap = new frmHoidap("Không thể chọn ngày bắt đầu nhỏ hơn ngày kết thúc", Hopthoai.frmHoidap.CO.an, Hopthoai.frmHoidap.KHONG.an, Hopthoai.frmHoidap.THOAT.an, Hopthoai.frmHoidap.HINH.baoloi);
                    hoidap.ShowDialog();
                }
                
            }
            
        }
        private void btnexcel_Click(object sender, EventArgs e)
        {
            XuatGIONGUON(1);
        }
        public void TasStartExcel([Optional, DefaultParameterValue("Sheet1")] string SheetName)
        {
            try
            {
                ExApp = new ApplicationClass();
                TasWBook = ExApp.Workbooks.Add(Missing.Value);
                TasWSheet = (Worksheet)ExApp.Worksheets["Sheet1"];
                TasWSheet.Name = SheetName;
                ExApp.Visible = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        public System.Drawing.Point DoPhanGiaiManhinh()
        {
            System.Drawing.Point p = new System.Drawing.Point();
            Size s = SystemInformation.WorkingArea.Size;
            p.X = Size.Width / 2;
            p.Y = Size.Height / 2;
            return p;
        }
        public void XuatGIONGUON(int vitri)
        {
            TasStartExcel("GIONGUON");
            int count = dt.Rows.Count;
            // doc do phan giai man hinh
            frmwaiting f = new frmwaiting();
            f.Show();
            f.lbgoi.Text = "Đang duyệt...";
            f.lbphantram.Visible = true;
            f.tocdo.Visible = true;
            f.tocdo.Maximum = count;
            f.Location = DoPhanGiaiManhinh();
            f.Activate();
            int i = 0;
            System.Windows.Forms.Application.DoEvents();
            if(dt.Rows.Count!=0)
            {
                try
                {
                    Worksheet tasWSheet = TasWSheet;
                    tasWSheet.get_Range("E" + Conversions.ToString((long)(2)) + ":I" + Conversions.ToString((long)(2)), Missing.Value).Merge(Missing.Value);
                    tasWSheet.get_Range("E" + Conversions.ToString((long)(2)) + ":I" + Conversions.ToString((long)(2)), Missing.Value).Borders.LineStyle = 1;
                    tasWSheet.get_Range("E" + Conversions.ToString((long)(2)), Missing.Value).set_Value(Missing.Value, "BẢNG GIỜ NGUỒN");
                    tasWSheet.get_Range("E" + Conversions.ToString((long)(2)), Missing.Value).Font.Bold = true;
                    tasWSheet.get_Range("E" + Conversions.ToString((long)(2)), Missing.Value).Font.Size = 16;
                    tasWSheet.get_Range("E" + Conversions.ToString((long)(2)), Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
                    tasWSheet.get_Range("E" + Conversions.ToString(4), Missing.Value).set_Value(Missing.Value, "STT");
                    tasWSheet.get_Range("F" + Conversions.ToString(4), Missing.Value).set_Value(Missing.Value, "Mã Nhân Viên");
                    tasWSheet.get_Range("G" + Conversions.ToString(4), Missing.Value).set_Value(Missing.Value, "Tên Nhân Viên");
                    tasWSheet.get_Range("H" + Conversions.ToString(4), Missing.Value).set_Value(Missing.Value, "Ngày");
                    tasWSheet.get_Range("I" + Conversions.ToString(4), Missing.Value).set_Value(Missing.Value, "Giờ");
                    tasWSheet.get_Range("E4", Missing.Value).Borders.LineStyle = 1;
                    tasWSheet.get_Range("E4", Missing.Value).Font.Bold = true;
                    tasWSheet.get_Range("E4", Missing.Value).Font.Size = 12;
                    NewLateBinding.LateSetComplex(tasWSheet.Columns["E", Missing.Value], null, "ColumnWidth", new object[] { 10 }, null, null, false, true);
                    tasWSheet.get_Range("E4", Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
                    tasWSheet.get_Range("F4", Missing.Value).Borders.LineStyle = 1;
                    tasWSheet.get_Range("F4", Missing.Value).Font.Bold = true;
                    tasWSheet.get_Range("F4", Missing.Value).Font.Size = 12;
                    tasWSheet.get_Range("F4", Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
                    NewLateBinding.LateSetComplex(tasWSheet.Columns["F", Missing.Value], null, "ColumnWidth", new object[] { 20 }, null, null, false, true);
                    tasWSheet.get_Range("G4", Missing.Value).Borders.LineStyle = 1;
                    tasWSheet.get_Range("G4", Missing.Value).Font.Bold = true;
                    tasWSheet.get_Range("G4", Missing.Value).Font.Size = 12;
                    tasWSheet.get_Range("G4", Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
                    NewLateBinding.LateSetComplex(tasWSheet.Columns["G", Missing.Value], null, "ColumnWidth", new object[] { 20 }, null, null, false, true);
                    tasWSheet.get_Range("H4", Missing.Value).Borders.LineStyle = 1;
                    tasWSheet.get_Range("H4", Missing.Value).Font.Bold = true;
                    tasWSheet.get_Range("H4", Missing.Value).Font.Size = 12;
                    tasWSheet.get_Range("H4", Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
                    NewLateBinding.LateSetComplex(tasWSheet.Columns["H", Missing.Value], null, "ColumnWidth", new object[] { 30 }, null, null, false, true);
                    tasWSheet.get_Range("I4", Missing.Value).Borders.LineStyle = 1;
                    tasWSheet.get_Range("I4", Missing.Value).Font.Bold = true;
                    tasWSheet.get_Range("I4", Missing.Value).Font.Size = 12;
                    tasWSheet.get_Range("I4", Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
                    NewLateBinding.LateSetComplex(tasWSheet.Columns["I", Missing.Value], null, "ColumnWidth", new object[] { 20 }, null, null, false, true);
                    foreach (DataRow dr in dt.Rows)
                    {

                        f.tocdo.Value = i;
                        f.lbphantram.Text = Conversions.ToString((double)(Math.Round((double)(((double)i) / ((double)count)), 2) * 100.0)) + "%";
                        System.Windows.Forms.Application.DoEvents();
                        f.Activate();
                        string MANV = dr["MANV"].ToString();
                        string TENV = dr["TENNV"].ToString();
                        string dtngay = dr["NGAY"].ToString();
                        string GIO = dr["GIO"].ToString();
                        tasWSheet.get_Range("E" + Conversions.ToString(i + 5), Missing.Value).set_Value(Missing.Value, "'" + (i + 1).ToString());
                        tasWSheet.get_Range("E" + Conversions.ToString(i + 5), Missing.Value).Borders.LineStyle = 1;
                        tasWSheet.get_Range("E" + Conversions.ToString(i + 5), Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
                        tasWSheet.get_Range("F" + Conversions.ToString(i + 5), Missing.Value).set_Value(Missing.Value, "'" + MANV);
                        tasWSheet.get_Range("F" + Conversions.ToString(i + 5), Missing.Value).Borders.LineStyle = 1;
                        tasWSheet.get_Range("G" + Conversions.ToString(i + 5), Missing.Value).set_Value(Missing.Value, "'" + TENV);
                        tasWSheet.get_Range("G" + Conversions.ToString(i + 5), Missing.Value).Borders.LineStyle = 1;
                        tasWSheet.get_Range("H" + Conversions.ToString(i + 5), Missing.Value).set_Value(Missing.Value, "'" + dtngay);
                        tasWSheet.get_Range("H" + Conversions.ToString(i + 5), Missing.Value).Borders.LineStyle = 1;
                        tasWSheet.get_Range("I" + Conversions.ToString(i + 5), Missing.Value).set_Value(Missing.Value, "'" + GIO);
                        tasWSheet.get_Range("I" + Conversions.ToString(i + 5), Missing.Value).Borders.LineStyle = 1;
                        tasWSheet.get_Range("I" + Conversions.ToString(i + 5), Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
                        i++;
                    }
                }
                catch(Exception e)
                {
                    MessageBox.Show(e.Message.ToString());
                    f.Close();
                }
            }
            f.Close();
        }
        public delegate void _Close();
        public event _Close Close;
        private void simpleButton3_Click(object sender, EventArgs e)
        {
            if (this.Close != null)
                this.Close();
        }
    }
}
